//
//  NetworkingTools.m
//  OC-微博
//
//  Created by Apple on 15/11/10.
//  Copyright © 2015年 Apple. All rights reserved.
//

#import "NetworkingTools.h"
@interface NetworkingTools()
//返回有效的token值
@property (nonatomic, strong) NSMutableDictionary *tokenDic;
@end
@implementation NetworkingTools
//设置单例
+ (instancetype)shardTools{
    static  NetworkingTools *instance  ;
    static dispatch_once_t once;
    dispatch_once(&once, ^{
        instance = [[self alloc]initWithBaseURL:nil];
        instance.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/plain", @"text/json", @"text/javascript", nil];
        
        
    });
    return instance;
}
#pragma mark /*******************  Home相关方法  *******************/
- (void)loadStatuesInfofinished:(HMRequestCallBack)finish {
    NSString *urlstring = @"https://api.weibo.com/2/statuses/home_timeline.json";
    //  获取 token 字典
     NSMutableDictionary *params = [NSMutableDictionary dictionary];
        [self tokenRequest:GET URLString:urlstring parameters:params finished:finish];
    
    
}

#pragma mark /*******************  用户相关方法  *******************/
- (void)loadUserInfo:(NSString *)uid finished:(HMRequestCallBack)finished {
    NSString *urlstring = @"https://api.weibo.com/2/users/show.json";
    //  获取 token 字典
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
        params[@"uid"] = uid;
    [self tokenRequest:GET URLString:urlstring parameters:params finished:finished];
    
    
}
#pragma mark /*******************  OAuth 相关方法  *******************/
/// 加载 AccessToken 获取授权
- (void)loadAccessToken:(NSString *)code finished:(HMRequestCallBack)finished {
    NSString *urlstring = @"https://api.weibo.com/oauth2/access_token";
    NSDictionary *params = @{@"client_id": self.appKey,
                             @"client_secret": self.appSecret,
                             @"grant_type": @"authorization_code",
                             @"code": code,
                             @"redirect_uri": self.redirectUrl};
    [self request:POST URLString:urlstring parameters:params finished:finished];
}
#pragma mark /*******************  封装网络请求  *******************/
- (BOOL)appendToken:( NSDictionary **)parameters {
    // 1> 判断 token 是否为nil
    NSString *token = [UserAccountViewModel shardViewModel].token;
    if (token == nil){

        return NO;
    }

// 2> 判断参数字典是否有值
    if (*parameters == nil) {
        *parameters = [NSDictionary dictionary];
    }
     // 3> 设置 token
    
   [*parameters setValue:token forKey:@"access_token"];
return YES;

}
- (void)tokenRequest:(HMRequestMethod)method URLString:(NSString *)urlstring parameters:(id)params finished:(HMRequestCallBack)finish {
 // 1> 如果追加 token 失败，直接返回
    if (![self appendToken:&params]) {
        
        NSLog(@"token为空");
        return ;
    }
// 2. 发起网络请求
    [self request:method URLString:urlstring parameters:params finished:finish];
}
- (void)request:(HMRequestMethod)method URLString:(NSString *)urlstring parameters:(id)params finished:(HMRequestCallBack)finish {
    void (^success)(NSURLSessionDataTask *task,id) = ^(NSURLSessionDataTask *task,id result) {
        finish(result,nil);
        
    };
    void (^failure)(NSURLSessionDataTask *task,id) = ^(NSURLSessionDataTask *task,id error) {
        NSLog(@"%@",error);
        finish(nil,error);
        
    };
    if (method == GET) {
        [self GET:urlstring parameters:params success:success failure:failure];
    }else{
        [self POST:urlstring parameters:params success:success failure:failure];
    }
}
#pragma mark /*******************  重写属性赋值  *******************/
- (NSString *)appKey {
    if (_appKey == nil) {
        
        
        _appKey = @"1965312787";
    }
    return _appKey;
}
- (NSString *)appSecret {
    _appSecret = @"380077fb39c24df4cc4e5898a3de36fc";
    return _appSecret;
}
- (NSString *)redirectUrl {
    _redirectUrl = @"http://www.baidu.com";
    return _redirectUrl;
}
- (NSURL *)oauth {
    _oauth =  [NSURL URLWithString:[NSString stringWithFormat:@"https://api.weibo.com/oauth2/authorize?client_id=%@&redirect_uri=%@",self.appKey,self.redirectUrl]];
    return _oauth;
}
@end
